<?php
/**
 * TODO продумать вопрос безопасности, где необходимо фильтровать переменные.
 * @since 27.08.2011
 * @author Akulov Denis <akulov.d.g@gmail.com>
 */
class MVC_View_Element implements MVC_View_Element_Interface {

    private $_strViewName = '';


    private $_arValues = array();


    private $_objConfig;


    public function __construct ($strViewName, Config_Interface $objConfig) {
        $this->_objConfig = $objConfig;
        $this->_strViewName = $strViewName;
    }


    public function _addVariable ($strName, $mixValue) {
        $this->_arValues[$strName] = $mixValue;
        return $this;
    }


    public function _addVariableIfNotEmpty ($strName, $mixValue) {
        if (!empty($mixValue)) {
            $this->_arValues[$strName] = $mixValue;
        }
        return $this;
    }


    public function _addVariableFromContainer (
        Container_OnlyRead_Interface $objContainer,
        $strVariableName
    ) {
        if ($objContainer->isExists($strVariableName)) {
            $this->_arValues[$strVariableName]
                = $objContainer->get($strVariableName);
        }

        return $this;
    }


    public function render() {
        $this->addSubview($this->getViewName(), $this->getValues());
    }


    private function addSubview ($strName, $arValues) {
        foreach ($arValues as $name => $value) {
            $$name = $value;
        }
        require $this->getConfig()->getAttribute('FW_PATHS', 'PROTOTYPE').
                'MVC/V/'.str_replace('_','/',$strName).'.php';
    }


    /**
     * @return Config_Interface
     */
    private function getConfig() {
        return $this->_objConfig;
    }


    private function getValues () {
        return $this->_arValues;
    }


    private function getViewName () {
        return $this->_strViewName;
    }

}